package com.managedBean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.primefaces.event.SlideEndEvent;

import com.controller.ItemsController;
import com.dto.Item;
import com.dto.MoreSize;
import com.managedBean.cart.CartManagedBean;
import com.utils.JsfUtils;

@ManagedBean(name = "itemsManagedBean")
@ViewScoped
public class ItemsManagedBean implements Serializable {

	
	private static final long serialVersionUID = 5159723507863840099L;
	private List<Item> itemsList;
	private int priceMinBoundary;
	private int priceMaxBoundary;
	private int priceMinRange;
	private int priceMaxRange;
	Map<String, String> brandMap = new LinkedHashMap<String, String>();
	private transient List<String> selectedBrandList;
	private Item selectedItem;
	private MoreSize selectedMoreSizeItem;
	private List<Item> filteredList;
	private String menuId;
	@ManagedProperty(value = "#{itemsController}")
	private ItemsController itemsController;
//	private StreamedContent itemImage;

	public ItemsManagedBean() {
		super();
		itemsList = new ArrayList<Item>();
		selectedBrandList = new ArrayList<String>();
		filteredList = new ArrayList<Item>();
	}

	@PostConstruct
	public void populateItems() {
		String l2_id = (String) FacesContext.getCurrentInstance()
				.getExternalContext().getRequestParameterMap().get("l2");
		System.out.println("l2_id=" + l2_id);
		String l3_id = (String) FacesContext.getCurrentInstance()
		.getExternalContext().getRequestParameterMap().get("l3");
		System.out.println("l3_id=" + l3_id);
		
		// for (int i = 1; i < 50; i++) {
		// Item item = new Item();
		// item.setName("Item " + i);
		// item.setBrand("Brand " + i);
		// item.setItemId(i);
		// List<MoreSize> moreSizesList = new ArrayList<MoreSize>();
		// for (int j = 1; j < 10; j++) {
		// MoreSize size = new MoreSize();
		// size.setPrice(Math.round((Math.random() * 10000) / 100));
		// size.setSizeName(j * 100 + "gm");
		// size.setMoreSizeItemId(j);
		// moreSizesList.add(size);
		// }
		// item.setMoreSizesList(moreSizesList);
		// if (item.getMoreSizesList().size() > 0) {
		// item.setPrice(item.getMoreSizesList().get(0).getPrice());
		// item.setSize(item.getMoreSizesList().get(0).getSizeName());
		// }
		// itemsList.add(item);
		// }
		String level="";
		Integer menuId;
		if(null!=l2_id){
			level="l2_id";
			menuId=Integer.parseInt(l2_id.trim());
		}else{
			level="l3_id";
			menuId=Integer.parseInt(l3_id.trim());
		}
		itemsList = itemsController.findByMenu(level,menuId);
		populateRefinedByValues();
	}

	public void displaySelectedSize() {
		System.out.println(selectedItem.getName());
		System.out.println(selectedMoreSizeItem.getSizeName());
		selectedItem.setSize(selectedMoreSizeItem.getSizeName());
		selectedItem.setPrice(selectedMoreSizeItem.getPrice());
		for (Item var : filteredList) {
			if (var.getItemId() == selectedItem.getItemId()) {
				var = selectedItem;
			}
		}
	}

	public void addSelectedSize() {
		System.out.println(selectedItem.getName());
		System.out.println(selectedMoreSizeItem.getSizeName());
		selectedItem.setSize(selectedMoreSizeItem.getSizeName());
		selectedItem.setPrice(selectedMoreSizeItem.getPrice());
		selectedItem.setItemId(selectedMoreSizeItem.getMoreSizeItemId());
		CartManagedBean cart = (CartManagedBean) JsfUtils
				.getBeanInstanceFromFacesContext("cart");
		cart.addToCart(selectedItem);
	}

	private void populateRefinedByValues() {

		for (Item var : itemsList) {
			brandMap.put(var.getBrand(), var.getBrand());
			selectedBrandList.add(var.getBrand());
			if (var.getPrice() > priceMaxBoundary) {
				priceMaxBoundary = (int) var.getPrice();
			}
			if (var.getPrice() < priceMinBoundary) {
				priceMinBoundary = (int) var.getPrice();
			}
		}

		priceMinRange = priceMinBoundary;
		priceMaxRange = priceMaxBoundary;
		filteredList.addAll(itemsList);
	}

	public void onSlideEnd(SlideEndEvent event) {
		filter();
	}

	private void filter() {
		filteredList.clear();
		for (Item var : itemsList) {
			for (String brand : selectedBrandList) {
				if (var.getPrice() > priceMinRange
						&& var.getPrice() < priceMaxRange
						&& var.getBrand().equalsIgnoreCase(brand)) {
					filteredList.add(var);
				}
			}
		}

	}

	public void filteryByBrand() {
		filter();
	}

	public void sizeChangeListener() {
		String id = FacesContext.getCurrentInstance().getExternalContext()
				.getRequestParameterMap().get("itemId");
		for (Item item : itemsList) {
			if (String.valueOf(item.getItemId()).equalsIgnoreCase(id)) {
				System.out.println(item.getMoreSizeItemId());
				boolean found = false;
				if (!found) {
					for (MoreSize moreSize : item.getMoreSizesList()) {
						if (item.getMoreSizeItemId() == moreSize
								.getMoreSizeItemId()) {
//							item.setItemImage(moreSize.getItemImage());
							item.setItemImagePath(moreSize.getImagePath());
							item.setPrice(moreSize.getPrice());
							item.setSrp(moreSize.getSrp());
							item.setOffer(moreSize.getOffer() + "");
							item.setSize(moreSize.getSizeName());
							found = true;
							break;
						}
					}
				}
				if(found){
					break;
				}
			}
		}
	}

	public void go() {
		System.out.println(priceMinRange);
		System.out.println(priceMaxRange);
	}

	public void addToCart() {
		CartManagedBean cart = (CartManagedBean) JsfUtils
				.getBeanInstanceFromFacesContext("cart");
		cart.addToCart(selectedItem);
	}
	
	/**
	 * @return the itemsList
	 */
	public List<Item> getItemsList() {
		return itemsList;
	}

	/**
	 * @param itemsList
	 *            the itemsList to set
	 */
	public void setItemsList(List<Item> itemsList) {
		this.itemsList = itemsList;
	}

	/**
	 * @return the priceMinBoundary
	 */
	public int getPriceMinBoundary() {
		return priceMinBoundary;
	}

	/**
	 * @param priceMinBoundary
	 *            the priceMinBoundary to set
	 */
	public void setPriceMinBoundary(int priceMinBoundary) {
		this.priceMinBoundary = priceMinBoundary;
	}

	/**
	 * @return the priceMaxBoundary
	 */
	public int getPriceMaxBoundary() {
		return priceMaxBoundary;
	}

	/**
	 * @param priceMaxBoundary
	 *            the priceMaxBoundary to set
	 */
	public void setPriceMaxBoundary(int priceMaxBoundary) {
		this.priceMaxBoundary = priceMaxBoundary;
	}

	/**
	 * @return the priceMinRange
	 */
	public int getPriceMinRange() {
		return priceMinRange;
	}

	/**
	 * @param priceMinRange
	 *            the priceMinRange to set
	 */
	public void setPriceMinRange(int priceMinRange) {
		this.priceMinRange = priceMinRange;
	}

	/**
	 * @return the priceMaxRange
	 */
	public int getPriceMaxRange() {
		return priceMaxRange;
	}

	/**
	 * @param priceMaxRange
	 *            the priceMaxRange to set
	 */
	public void setPriceMaxRange(int priceMaxRange) {
		this.priceMaxRange = priceMaxRange;
	}

	/**
	 * @return the brandMap
	 */
	public Map<String, String> getBrandMap() {
		return brandMap;
	}

	/**
	 * @param brandMap
	 *            the brandMap to set
	 */
	public void setBrandMap(Map<String, String> brandMap) {
		this.brandMap = brandMap;
	}

	/**
	 * @return the selectedBrandList
	 */
	public List<String> getSelectedBrandList() {
		return selectedBrandList;
	}

	/**
	 * @param selectedBrandList
	 *            the selectedBrandList to set
	 */
	public void setSelectedBrandList(List<String> selectedBrandList) {
		this.selectedBrandList = selectedBrandList;
	}

	/**
	 * @return the selectedItem
	 */
	public Item getSelectedItem() {
		return selectedItem;
	}

	/**
	 * @param selectedItem
	 *            the selectedItem to set
	 */
	public void setSelectedItem(Item selectedItem) {
		this.selectedItem = selectedItem;
	}

	/**
	 * @return the filteredList
	 */
	public List<Item> getFilteredList() {
		return filteredList;
	}

	/**
	 * @param filteredList
	 *            the filteredList to set
	 */
	public void setFilteredList(List<Item> filteredList) {
		this.filteredList = filteredList;
	}

	/**
	 * @return the menuId
	 */
	public String getMenuId() {
		return menuId;
	}

	/**
	 * @param menuId
	 *            the menuId to set
	 */
	public void setMenuId(String menuId) {
		this.menuId = menuId;
	}

	/**
	 * @return the selectedMoreSizeItem
	 */
	public MoreSize getSelectedMoreSizeItem() {
		return selectedMoreSizeItem;
	}

	/**
	 * @param selectedMoreSizeItem
	 *            the selectedMoreSizeItem to set
	 */
	public void setSelectedMoreSizeItem(MoreSize selectedMoreSizeItem) {
		this.selectedMoreSizeItem = selectedMoreSizeItem;
	}

	/**
	 * @return the itemsController
	 */
	public ItemsController getItemsController() {
		return itemsController;
	}

	/**
	 * @param itemsController
	 *            the itemsController to set
	 */
	public void setItemsController(ItemsController itemsController) {
		this.itemsController = itemsController;
	}

//	public StreamedContent getItemImage() throws IOException {
//		FacesContext facesContext = FacesContext.getCurrentInstance();
//
//		if (facesContext.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
//			// So, we're rendering the HTML. Return a stub StreamedContent so
//			// that it will generate right URL.
//			return new DefaultStreamedContent();
//		} else {
//			// So, browser is requesting the image. Return a real
//			// StreamedContent
//			// with the image bytes.
//			String id = facesContext.getExternalContext()
//					.getRequestParameterMap().get("imageId");
//			Item item = new Item();
//			for (Item var : itemsList) {
//				if (String.valueOf(var.getItemId()).equalsIgnoreCase(id)) {
//					item = var;
//				}
//			}
//			DefaultStreamedContent img = new DefaultStreamedContent();
//			try {
//				if (null != item && null != item.getItemImage()) {
//					ByteArrayInputStream bis = new ByteArrayInputStream(
//							item.getItemImage());
//					img = new DefaultStreamedContent(bis);
//					bis.close();
//				}
//			} catch (IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//			return img;
//
//		}
//	}

}
